
function getAccessToken() {
  return new Promise((resolve,reject)=> {
    wx.request({
      url: 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=ds1UZTQOVA7heW6be9lSY4P0&client_secret=8S2qXNZcjKhQL6V7vA3YNdVQw59n8MvS',
      method: "post",
      success: (result)=> {
       resolve(result.data.access_token)
      },
      fail:(error) => {
        reject(error)
      }
    })
  })
}

function clientRequest(access_token,image) {
  return new Promise((resolve,reject)=> {
    wx.request({
      url: `https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate?access_token=${access_token}`,
      method: "post",
      header: {
        "Content-Type":"application/x-www-form-urlencoded"
      },
      data:{
        image
      },
      success: (result)=> {
        resolve(result.data.words_result)
      },
      fail:(error) => {
        reject(error)
      }
    })
  })
}

function recognitionLicense(image) {
  return getAccessToken().then((access_token)=>{
    return clientRequest(access_token,image)
  })
}

module.exports.getAccessToken = getAccessToken;
module.exports.recognitionLicense = recognitionLicense;